package chapter6;

import java.util.Scanner;

/****************************************************************
 * Author：icynlau
 * Description: 平方根的近似求法---巴比伦法
 * newGuess=(lastGuess+n/lastGuess)/2;
 * Date:2021/4/4
 * ***************************************************************
 */

public class exc6_22 {
	
	public static void main(String[]  args) {
		
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter a numebr:  ");
		long n=input.nextLong();
		
		System.out.print(n+" 的平方根约为 "+sqrt(n));
		
		
	}
	
	public static double sqrt(long n) {
		
		double lastGuess=1;
		double newGuess=(lastGuess+n/lastGuess)/2;
		while(Math.abs(newGuess-lastGuess)>0.0001){
			lastGuess=newGuess;
			newGuess=(lastGuess+n/lastGuess)/2;
			
		}
		
		return newGuess;
		
	}

}
